package com.gelpag.base.business.role;

import been.RoleInfo;
import been.UserInfo;
import com.gelpag.base.business.bean.PageBean;
import com.gelpag.base.business.common.CommonBase;
import com.gelpag.base.business.menu.MenuInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.Const;
import entity.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tools.utils.Common;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {

    Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleService roleService;

    /**
     * 获取角色列表
     * @param request
     * @param roleInfo
     * @return
     */
    @PostMapping("/getRoleList")
    public Result getRoleList(HttpServletRequest request, RoleInfo roleInfo) {

        String token = request.getHeader("token");
        try {
            UserInfo userInfo = CommonBase.getUserInFromToken(token);
            if (userInfo == null) {
                return new Result(Const.RESULT_F1,Const.ERROR_MSG_F1,token,"");
            }

            int pageNum = Common.getPageNum(roleInfo.getPageNum());
            int pageSize = Common.getPageSize(roleInfo.getPageSize());
            Page<RoleInfo> page = PageHelper.startPage(pageNum, pageSize);
            roleService.getRoleList();
            PageBean<RoleInfo> pageData = new PageBean<>(pageNum, pageSize, page.getTotal(), page.getPages(), page.getResult());

            return new Result(Const.RESULT_0,Const.ERROR_MSG_0,token,pageData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1,Const.ERROR_MSG_1,token,"");
        }
    }

    @PostMapping("/saveRole")
    public Result saveRole(HttpServletRequest request, RoleInfo roleInfo) {

        String token = request.getHeader("token");
        try {
            UserInfo userInfo = CommonBase.getUserInFromToken(token);
            if (userInfo == null) {
                return new Result(Const.RESULT_F1,Const.ERROR_MSG_F1,token,"");
            }
            //参数验证
            Map<String, String> checkMap = isPassSaveRole(roleInfo);
            if (Const.NOT_NOMAL_PARAM.equals(checkMap.get(Const.PARAM_KEY))) {
                return new Result(Const.RESULT_1, checkMap.get(Const.PARAM_RET_KEY),token, "");
            }
            roleService.saveRole(userInfo, roleInfo);
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0,token,"");

        } catch (Exception e) {
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1,token,"");
        }
    }

    private Map<String, String> isPassSaveRole(RoleInfo roleInfo) {
        Map<String, String> checkMap = new HashMap<>();
        //默认返回正常
        checkMap.put(Const.PARAM_KEY, Const.NOMAL_PARAM);
        //验证role_name是否为空
        if (Common.isNullString(roleInfo.getRole_name())) {
            checkMap.put(Const.PARAM_KEY,Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY,"角色名称不能为空！");
            return checkMap;
        }
        //验证描述是否为空
        if (Common.isNullString(roleInfo.getRole_discp())) {
            checkMap.put(Const.PARAM_KEY,Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY,"角色描述不能为空！");
            return checkMap;
        }
        return checkMap;
    }

    /**
     * 获取权限列表
     * @param request
     * @param role_id
     * @return
     */
    @PostMapping("/getAuthTree")
    public Result getAuthTree(HttpServletRequest request, String role_id) {

        String token = request.getHeader("token");

        try {
            //参数验证
            Map<String, String> checkMap = isPassGetAuthTree(role_id);
            if (Const.NOT_NOMAL_PARAM.equals(checkMap.get(Const.PARAM_KEY))) {
                return new Result(Const.RESULT_1,checkMap.get(Const.PARAM_RET_KEY),token,"");
            }
            List<MenuInfo> list = roleService.getAuthTree(role_id);
            return new Result(Const.RESULT_0,Const.ERROR_MSG_0,token,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1,Const.ERROR_MSG_1,token,"");
        }
    }

    private Map<String, String> isPassGetAuthTree(String role_id) {

        Map<String, String> checkMap = new HashMap<>();
        //默认返回正常
        checkMap.put(Const.PARAM_KEY,Const.NOMAL_PARAM);
        if (Common.isNullString(role_id)) {
            checkMap.put(Const.PARAM_KEY,Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY,"role_id不能为空！");
            return checkMap;
        }
        return checkMap;
    }

    @PostMapping("/saveAuthTree")
    public Result saveAuthTree(HttpServletRequest request, @RequestBody Map<String, Object> map) {

        String token = request.getHeader("token");
        String role_id = (String) map.get("role_id");
        String resource_ids = (String)map.get("resource_ids");
        try {
            roleService.saveAuthTree(role_id, resource_ids);
            return new Result(Const.RESULT_0,Const.ERROR_MSG_0,token,"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1,Const.ERROR_MSG_1,token,"");
        }
    }

    /**
     * 删除角色
     * @param request
     * @param role_id
     * @return
     */
    @PostMapping("/deleteRoleById")
    public Result deleRoleById(HttpServletRequest request, String role_id) {

        String token = request.getHeader("token");
        //参数验证
        try {
            Map<String, String> checkMap = isPassDeleteRoleById(role_id);
            if (Const.NOT_NOMAL_PARAM.equals(checkMap.get(Const.PARAM_KEY))) {
                return new Result(Const.RESULT_1, checkMap.get(Const.PARAM_RET_KEY), token,"");
            }
            roleService.deleteRoleById(role_id);
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0,"","");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1, token, "");
        }
    }

    private Map<String, String> isPassDeleteRoleById(String role_id) {
        Map<String, String> checkMap = new HashMap<>();
        //默认返回正常
        checkMap.put(Const.PARAM_KEY, Const.NOMAL_PARAM);
        if (Common.isNullString(role_id)) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "role_id不能为空！");
            return checkMap;
        }
        //检查是否存在关联，检查表：sys_resource_role, sys_user_role, sys_role_menu
        if (roleService.countRoleRelation(role_id) > 0) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "存在关联数据，不能删除！");
            return checkMap;
        }
        return checkMap;
    }
}
